1 00:00:00,570 --> 00:00:01,200 Hey there! 2 00:00:01,200 --> 00:00:05,520 In this lecture, I would like to show you another neat module script that allows you to create your 3 00:00:05,520 --> 00:00:10,530 own events for your objects and classes that basically acts just like an RRB script signal. 4 00:00:10,530 --> 00:00:15,690 These events can be useful in many ways because they allow us to continue making our code event driven. 5 00:00:15,690 --> 00:00:20,730 For example, if you had a vehicle in your game that was controlled by a script that created an object 6 00:00:20,730 --> 00:00:26,010 for it, that object can store events that would fire when specific things happened with the vehicle, 7 00:00:26,010 --> 00:00:30,510 such as when the throttle was pressed down or an occupant went inside the vehicle, and so on. 8 00:00:30,510 --> 00:00:36,840 This module script is known as Good signal, basically, and it's a class that allows us to imitate 9 00:00:36,840 --> 00:00:39,750 or mimic what an RRB script signal does. 10 00:00:39,750 --> 00:00:44,790 So we can connect to a signal, we can disconnect from it and things of that nature. 11 00:00:44,790 --> 00:00:50,820 We can also use this module script to create custom events for our classes and custom services. 12 00:00:50,820 --> 00:00:53,580 Just like how Roblox services have their own events. 13 00:00:53,580 --> 00:00:59,070 For example, the player service has events like player added and player removing that can be connected 14 00:00:59,070 --> 00:00:59,610 to. 15 00:00:59,610 --> 00:01:04,770 If we created our own custom service and we wanted to create events for it, then this script is perfect 16 00:01:04,770 --> 00:01:06,120 for that task. 17 00:01:06,120 --> 00:01:11,760 The API for this module is very simple and again contains the same functions for connecting to an event 18 00:01:11,760 --> 00:01:13,200 or yielding for an event. 19 00:01:13,200 --> 00:01:18,930 So if we look inside of this module real quick, as you can see we can disconnect a connection. 20 00:01:18,930 --> 00:01:22,230 We have functions for connecting to a signal. 21 00:01:22,230 --> 00:01:27,300 We can disconnect actually all of the connections to a signal, which is pretty neat. 22 00:01:27,300 --> 00:01:31,290 We can fire a signal and pass any kind of arguments we would like. 23 00:01:31,290 --> 00:01:36,720 We can wait for a signal to execute, just like we could with a normal RRB script signal, and we can 24 00:01:36,720 --> 00:01:39,180 even connect a function to execute once. 25 00:01:39,180 --> 00:01:44,640 So when the signal fires, it will execute the function, and then it'll disconnect the function so 26 00:01:44,640 --> 00:01:48,750 it doesn't, uh, run that function anymore when the signal fires again. 27 00:01:48,750 --> 00:01:51,000 So it's a one time function. 28 00:01:51,510 --> 00:01:57,540 So to create a new custom signal all we need to do is refer to that module script and use the new constructor 29 00:01:57,540 --> 00:02:00,480 and store the return from that constructor inside of a variable. 30 00:02:00,480 --> 00:02:04,560 So I'm going to refer to replicated storage where I have my module script stored. 31 00:02:06,290 --> 00:02:08,450 And then I can go ahead and get my signal. 32 00:02:08,450 --> 00:02:10,370 I'll just call this good signal. 33 00:02:10,980 --> 00:02:15,690 And we can require it from replicated storage and get our signal module script. 34 00:02:15,960 --> 00:02:18,960 And then we can go ahead and create our custom signal. 35 00:02:18,960 --> 00:02:21,480 I'll just create a variable called custom signal. 36 00:02:21,480 --> 00:02:23,130 And that's going to be equal to good signal. 37 00:02:23,130 --> 00:02:25,620 And we use the new constructor. 38 00:02:25,620 --> 00:02:30,480 And now we have our signal right here that can be connected to it can be fired. 39 00:02:30,480 --> 00:02:36,150 And we can do all sorts of different stuff with it, just like we would with a normal script signal. 40 00:02:36,540 --> 00:02:41,670 So for example, with our custom signal we can connect a function to it. 41 00:02:42,260 --> 00:02:46,460 And this function can get any amount of arguments when the signal is fired. 42 00:02:46,460 --> 00:02:48,050 And we'll just print that out here. 43 00:02:48,050 --> 00:02:53,000 We can just say this signal fired with the arguments. 44 00:02:53,000 --> 00:02:56,300 And then we'll just pass whatever arguments to this print function. 45 00:02:56,630 --> 00:03:00,710 We can also connect once to this signal. 46 00:03:00,710 --> 00:03:05,870 So we can use the once function, pass a function again get any arguments. 47 00:03:05,870 --> 00:03:08,780 And we can basically just print out the exact same thing here. 48 00:03:09,350 --> 00:03:13,760 And what I'm going to do is I'm going to refer to my custom signal and I'm going to fire it. 49 00:03:13,760 --> 00:03:19,040 So this is how we trigger the signal and execute these functions that are connected to it. 50 00:03:19,040 --> 00:03:22,610 And I'm just going to pass some random arguments like one, two and three. 51 00:03:22,790 --> 00:03:27,830 And when this signal first gets fired both of these should get printed out into the console. 52 00:03:27,830 --> 00:03:30,890 So we should have two of these identical print statements. 53 00:03:31,100 --> 00:03:33,800 And then I'm just going to wait for like five seconds. 54 00:03:34,100 --> 00:03:39,050 And then afterwards I'm going to fire the signal again and pass the same arguments. 55 00:03:39,050 --> 00:03:44,000 However, this time we should only get one statement printed in the console, because this one will 56 00:03:44,000 --> 00:03:49,730 get automatically disconnected because this function is only supposed to execute once, and then afterwards, 57 00:03:49,730 --> 00:03:54,410 let's say we're going to disconnect all of the functions connected to the signal. 58 00:03:54,410 --> 00:04:00,410 So that means if I were to copy this again and try to fire it again, none of these functions should 59 00:04:00,410 --> 00:04:02,720 execute because they were all disconnected. 60 00:04:03,230 --> 00:04:08,540 And then to demonstrate the wait functionality, what we can do is we can spawn a function in a new 61 00:04:08,540 --> 00:04:09,560 thread. 62 00:04:09,560 --> 00:04:14,180 And what we're going to do is refer to our custom signal, and we're going to wait or yield until the 63 00:04:14,180 --> 00:04:15,530 signal fires. 64 00:04:16,220 --> 00:04:21,080 And then afterwards we can print something like successfully yielded. 65 00:04:22,470 --> 00:04:24,840 Um until event fired. 66 00:04:25,380 --> 00:04:31,200 And what we'll do down here is wait for five seconds, and then we're going to fire our custom signal, 67 00:04:31,590 --> 00:04:35,520 which should allow us to get to this print statement. 68 00:04:35,700 --> 00:04:37,800 So now, if we run our game. 69 00:04:40,050 --> 00:04:44,370 As you can see, we get both print statements inside of our console and then we should only get one. 70 00:04:44,370 --> 00:04:44,880 There we go. 71 00:04:44,880 --> 00:04:46,380 We only got one that time. 72 00:04:47,280 --> 00:04:48,390 And then afterwards. 73 00:04:48,390 --> 00:04:50,190 As you can see, nothing else printed. 74 00:04:50,190 --> 00:04:54,570 But we did get the statement successfully yielded until event fired. 75 00:04:54,570 --> 00:04:58,770 So the print statements match up perfectly with what we wrote in our script. 76 00:04:58,770 --> 00:05:05,460 These two printed together at the exact same time, and then five seconds later, only one printed. 77 00:05:05,460 --> 00:05:10,770 Because this one was automatically disconnected, we disconnected all of the functions that were listening 78 00:05:10,800 --> 00:05:15,030 to our signal, which means this did absolutely nothing which we expect. 79 00:05:15,240 --> 00:05:18,210 And then we were waiting for our custom signal. 80 00:05:18,210 --> 00:05:23,220 And then once it fired, then we were able to reach this print statement here, which is exactly what 81 00:05:23,220 --> 00:05:23,850 happened. 82 00:05:24,300 --> 00:05:29,490 As you can see, this is a very simple and yet very powerful module script that allows you to implement 83 00:05:29,490 --> 00:05:33,660 your own events into your scripts without having to use something like Bindable events. 84 00:05:33,660 --> 00:05:39,420 This module is much better as it's not prone to memory leaks like Bindesboll's, and this is because 85 00:05:39,420 --> 00:05:42,780 bind labels can cause memory leaks if you forget to clean up connections. 86 00:05:42,780 --> 00:05:47,460 So this module script is a great alternative that I would highly recommend using in your games. 87 00:05:47,460 --> 00:05:49,680 I will see you in the next lecture.